fix(api): don't scope serializer querysets by AnonymousUser during schema generation#15344
Merged
Merged
Conversation
…hema generation The Finding and import serializers scope their endpoint/location PrimaryKeyRelatedField querysets to request.user. During OpenAPI schema generation drf-spectacular instantiates serializers with an AnonymousUser request, which is truthy, so the `if user` branch called get_authorized_*(user=AnonymousUser); filtering a PK field by the AnonymousUser instance raised "Field 'id' expected a number but got AnonymousUser". drf-spectacular then dropped the affected views, producing an operationId collision and a KeyError in the prefetch postprocessing hook. Treat an unauthenticated user as no user so the field falls back to an empty queryset, matching pre-existing behavior for anonymous/schema-time contexts. Add a regression test that renders the full OpenAPI v3 schema and gates that generation produces zero errors (drf-spectacular logs, rather than raises, when it drops a view -- so nothing caught this before). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Maffooch
force-pushed
the
fix/openapi-schema-anonymoususer-scoping
branch
from
July 23, 2026 06:18
89a111b to
1e14f6a
Compare
blakeaowens
approved these changes
Jul 23, 2026
devGregA
self-requested a review
July 23, 2026 16:03
devGregA
approved these changes
Jul 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[sc-13920]
What
Fixes OpenAPI schema generation crashing when the Finding / import serializers are introspected with an unauthenticated request.
Why
The Finding serializer (
dojo/finding/api/serializer.py) and the import serializer (dojo/api_v2/serializers.py) scope their endpoint/locationPrimaryKeyRelatedFieldquerysets torequest.user. During OpenAPI schema generation drf-spectacular builds serializers with anAnonymousUserrequest.AnonymousUseris truthy, so theif userbranch calledget_authorized_*(user=AnonymousUser), and filtering a PK field by theAnonymousUserinstance raised:drf-spectacular then ignored the affected views (
FindingViewSet/importers), which produced anoperationId "findings_retrieve"collision and aKeyError: 'content'in the prefetch postprocessing hook — breaking schema-integrity tests for downstream (Pro) consumers.Fix
Treat an unauthenticated user as "no user" so the field falls back to an empty queryset, matching the prior behavior for anonymous / schema-time contexts:
Applied at both scoping sites.
Verification
Generated the full OpenAPI schema and ran the schema-integrity suite: no schema-generation errors/warnings, no operationId collisions, no prefetch KeyError; full mock-based unit phase green (15,441 passed).
🤖 Generated with Claude Code